package com.pomelo.tiku.impls;

import android.content.Context;

import com.pomelo.tiku.beans.LoginBean;
import com.pomelo.tiku.beans.RegisterBean;
import com.pomelo.tiku.beans.ResponseData;
import com.pomelo.tiku.http.OkHttpProcessor;
import com.pomelo.tiku.interfaces.Contract;
import com.pomelo.tiku.interfaces.IHttpCallBack;
import com.pomelo.tiku.utils.GsonUtil;

import java.util.HashMap;

/**
 * @author liuxiangwang
 * @date .
 */
public class LoginModel implements Contract.ILoginModel {
    private Context mContext;
    public LoginModel(Context context) {
        this.mContext = context;
    }

    @Override
    public void login(String url, HashMap<String, String> header, HashMap<String, String> data, Boolean isFormBody, ILoginResult listener) {
        OkHttpProcessor.getInstance(mContext).postRequest(url, header, data, isFormBody, new IHttpCallBack() {
            @Override
            public void onStart() {
                if(listener !=null) {
                    listener.onStart();
                }
            }

            @Override
            public void onSuccess(ResponseData data) {
                if(listener != null) {
                    LoginBean loginBean = GsonUtil.parseJsonToBean(data.getData().toString(),LoginBean.class);
                    if(loginBean != null) {
                        listener.onSuccess(loginBean);
                    } else {
                        listener.onFailed("服务器返回数据格式有误");
                    }
                }
            }

            @Override
            public void onFailed(int code, String message) {
                if(listener != null) {
                    listener.onFailed(message);
                }
            }

            @Override
            public void onHttpFailed(String message) {
                if(listener != null) {
                    listener.onHttpFailed(message);
                }
            }
        });
    }
}
